package unicall.em.models.vo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import unicall.em.models.SipAccount;
import unicall.em.utils.StringUtil;

public class SipAccountSpec implements Specification<SipAccount>{

    private final SipAccount account;
    
    public SipAccountSpec(SipAccount account){
        this.account = account;
    }
    
    
    @Override
    public Predicate toPredicate(Root<SipAccount> root, CriteriaQuery<?> cq,
            CriteriaBuilder cb) {
        List<Predicate> predicates = new ArrayList<Predicate>();
        if (!StringUtil.isBlank(account.getClientNumber())) {
            predicates.add(
                    cb.like(cb.lower(root.get(SipAccount_.clientNumber)), account.getClientNumber().toLowerCase() + "%"));
        }
        if (!StringUtil.isBlank(account.getAgentId())) {
            predicates.add(cb.equal(root.get(SipAccount_.agentId), account.getAgentId()));
        }
        return cb.and(predicates.toArray(new Predicate[0]));
    }

    
}
